﻿using System;
using Mersiv.Lib.Wrapper;
using Mersiv.Lib.Wrapper.Command;
using Mersiv.Lib.Wrapper.Event;
using Mersiv.App.Client.Data;

namespace Mersiv.App.Client.Controller
{

  /// <summary>
  /// Runs the "hg status" polling.
  /// </summary>
  class HgPollController
  {


    public HgPollController()
    {
      this.hgRepositoryAddedEvent = new HgRepositoryEvent(dataModel_RepositoryAdded);
      this.hgRepositoryRemovedEvent = new HgRepositoryEvent(dataModel_RepositoryRemoved);
    }


    private HgRepositoryEvent hgRepositoryAddedEvent;
    private HgRepositoryEvent hgRepositoryRemovedEvent;


    public DataModel dataModel;
    public DataModel DataModel
    {
      set
      {
        if (this.dataModel != null)
        {
          this.dataModel.RepositoryAdded -= hgRepositoryAddedEvent;
          this.dataModel.RepositoryRemoved -= hgRepositoryRemovedEvent;
          //
          // TODO: remove "hg status" polls
          //
        }
        this.dataModel = value;
        this.dataModel.RepositoryAdded += hgRepositoryAddedEvent;
        this.dataModel.RepositoryRemoved += hgRepositoryRemovedEvent;
        //
        // TODO: Add an event handler to poll for "hg status" for each repository
        //
        // TODO: Use some sort of "HgPoll" object to manage the threading/polling/events
        //
        foreach (HgRepository hgRepository in this.dataModel.RepositoryList)
        {
        }
      }
      get
      {
        return this.dataModel;
      }
    }

    private void dataModel_RepositoryAdded(HgRepository hgRepository)
    {
      //
      // TODO: Add an event handler to poll for "hg status" for each repository
      //
    }
    private void dataModel_RepositoryRemoved(HgRepository hgRepository)
    {
    }




    /// <summary>
    /// TODO: Rename? New event?
    /// </summary>
    public event HgRepositoryEvent RepositoryFileUpdate;

    // Invoke the Changed event; called whenever list changes
    private void OnRepositoryFileUpdate(HgRepository hgRepository)
    {
      if (this.RepositoryFileUpdate != null)
      {
        this.RepositoryFileUpdate(hgRepository);
      }
    }



    


  }

}